<?php 
if( defined('DOM_PATH') === false  )
	define('DOM_PATH', dirname(__FILE__).'\\' );

if( defined('SIMPLE_TEST') === false  )
	define('SIMPLE_TEST', dirname(__FILE__).'/SimpleTest/' );
		
//require_once(SIMPLE_TEST.'autorun.php');

require_once( SIMPLE_TEST . 'unit_tester.php');
require_once( SIMPLE_TEST . 'mock_objects.php');
require_once( SIMPLE_TEST . 'collector.php');
require_once( SIMPLE_TEST . 'default_reporter.php');
require_once( SIMPLE_TEST . 'web_tester.php');
		
class DomTester extends TestSuite {

	public function __construct() {
		parent::TestSuite('DomTester');
		
		if( $_GET['group'] == 'All' ) {

				foreach( $this->getTestCases() as $case ) {
					if( is_array($case) ) {
						foreach( $case as $cc ) $this->addTestClass(  $cc.'TestCase' );
					} else {
						$this->addTestClass(  $case.'TestCase' );
					}
				}
				
		} else if( $_GET['case'] == 'All' ) {
			$cases = $this->getTestCases();
			if( $_GET['group'] ) {
				foreach( $cases[$_GET['group']] as $case ) $this->addTestClass(  $case.'TestCase' );
			} else {
				$this->addTestClass(  $_GET['case'].'TestCase' );
			}
		} else if( isset($_GET['case']) ){
			$this->addTestClass( $_GET['case'].'TestCase' );
		}
	}

	public function run() {
		parent::run( new DomSimpleTestReporter( $this ) );
	}
	
	public function getTestCases() {
		return array(
			'DomBase',
			'DomLocker',
			'DomBrowser',
			'DomDateTime' => array(
				'DomDateTime',
				'DomDateTimePeriod'
			),
			'DomController' => array(
				'DomController',
				'DomSite',
				'DomLanguage',
			),
			'DomCache',
			'DomSql',
			'DomAr'=>array(
				'DomAr',
				'DomArAnnotation',
				'DomArParser',
				'DomArBasicProperties',
				'DomArMisc',
				'DomArListeners',
				'DomArValidators',
				'DomArQuery',
				'DomArOne',
				'DomArOneToOne',
				'DomArOneToMany',
				'DomArManyToMany',
				'DomArAdvRelations',
				'DomArCustomTests',
				'DomArDates',
				'DomArFile',
				'DomArImage',
				'DomArAcl',
				'DomArSimpleOrder',
				'DomArSlug',
				'DomArHistory',
				'DomArChangeLog',
				'DomArSearchIndex',
				'DomArMultiDb',
			),
			'DomFileLogger',
			'DomDBLogger');
	}
}


class DomSimpleTestReporter extends HtmlReporter {
	
	private $caller;
	
	public function __construct( DomTester $caller ) {
        $this->HtmlReporter(  );
		$this->caller = $caller;
    }
	
	public function shouldInvoke( $label, $method ) {
		if( isset($_GET['test']) === true or isset($_GET['case']) === true) {
			return $method == $_GET['test'] or $_GET['test'] == 'All';
		} else {
			return false;
			//return parent::shouldInvoke($test_case, $method);
		}
	}
	
	public function paintHeader($test_name) {
		$this->sendNoCacheHeaders();
        print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
        print "<html>\n<head>\n<title>$test_name</title>\n";
        print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" .
                $this->_character_set . "\">\n";
        print "<style type=\"text/css\">\n";
        print $this->_getCss() . "\n";
        print "</style>\n";
        print "</head>\n<body>\n";
		print '<div style="border: 1px solid #CCCCCC; padding: 10px;">';
        print '<div style="background-color:#003366; color:#FFFFFF; font-size: 18px; font-weight:bold; padding: 8px;">'.$test_name.'</div>'."\n";
		print '<div style="border: 1px solid #CCCCCC; padding: 10px; margin:10px 0px 10px 0px;"> <strong>Libs:</strong> ';
		print '&nbsp;&nbsp;&nbsp;<a style="color:#003399; text-decoration:none; font-weight:bold;" href="?group=All&case=All&test=All">All</a>&nbsp;&nbsp;&nbsp;|';	
		foreach( $this->caller->getTestCases() as $key => $group ) {
			print '&nbsp;&nbsp;&nbsp;<a style="color:#003399; text-decoration:none; font-weight:bold;" href="?'.(is_array($group) ? 'group='.$key : 'case='.$group).'">'.(is_array($group) ? $key : $group).'</a>&nbsp;&nbsp;&nbsp;|';	
		}
		print '</div>';
		if( isset($_GET['group']) === true && $_GET['group'] != 'All' ) {
			print '<div style="border: 1px solid #CCCCCC; padding: 10px; margin:10px 0px 10px 0px;"> <strong>Group '.$_GET['group'].':</strong> ';
			print '<a style="color:#003399; text-decoration:none; font-size: 15px;" href="?group='.$_GET['group'].'&case=All&test=All"><strong>All</strong></a> | ';
			$classes = $this->caller->getTestCases();
			foreach($classes[$_GET['group']] as $className ) {
				print '<a style="color:#003399; text-decoration:none; font-size: 15px;" href="?group='.$_GET['group'].'&case='.$className.'"><strong>'.$className.'</strong></a> | ';
			}
			print '</div>';
		}
		
		if( isset($_GET['case']) === true && $_GET['case'] != 'All') {
			print '<div style="border: 1px solid #CCCCCC; padding: 10px; margin:10px 0px 10px 0px;">  <strong>Tests: '.$_GET['case'].'</strong>: ';
			print '<a style="color:#003399; text-decoration:none; font-size: 15px;" href="?'.($_GET['group'] ? 'group='.$_GET['group'].'&' : NULL).'case='.$_GET['case'].'&test=All">All</a> | ';
			foreach( get_class_methods( $_GET['case'].'TestCase' ) as $method ) {
				if( substr( $method, 0, 4) == 'test') {
					print '<a style="color:#003399; text-decoration:none; font-size: 15px;" href="?'.($_GET['group'] ? 'group='.$_GET['group'].'&' : NULL).'case='.$_GET['case'].'&test='.$method.'">'.$method.'</a> | ';
				}
			}
			print '</div>';
		}
        flush();
	}
	
	function paintFooter($test_name) {
        $colour = ($this->getFailCount() + $this->getExceptionCount() > 0 ? "red" : "green");
        print "<div style=\"";
        print "padding: 8px; margin-top: 1em; background-color: $colour; color: white;";
        print "\">";
        print $this->getTestCaseProgress() . "/" . $this->getTestCaseCount();
        print " test cases complete:\n";
        print "<strong>" . $this->getPassCount() . "</strong> passes, ";
        print "<strong>" . $this->getFailCount() . "</strong> fails and ";
        print "<strong>" . $this->getExceptionCount() . "</strong> exceptions.";
        print "</div>\n";
		print "</div>";
        print "</body>\n</html>\n";
    }
	
	//public function paintGroupStart($test_name, $size) { 
	//	echo '<h4> Run group: '.$test_name.'</h4>';
	//}
   
   // public function paintGroupEnd($test_name ) { 
	
	//}
	
	public function paintCaseStart( $test_case ) {
		echo '<span style="font-size:15px;">Run: <strong>'.$test_case.'</strong></span><br />';
		parent::paintCaseStart( $test_case, $size );
	}
	
    public function paintCaseEnd( $test_case ) {
		parent::paintCaseEnd( $test_case );
	}
	
	public function paintException( Exception $e ) {
		echo exceptionToString($e);
	}
 //   public function paintMethodStart( $test_name ) {
	
//	}
	
   // public function paintMethodEnd( $test_name ) {
	
	//}
	

}
?>